package com.kzd.demo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.kzd.sdk.callback.KZD_OnLoginListener;
import com.kzd.sdk.callback.KZD_OnPayListener;
import com.kzd.sdk.callback.KZD_OnRoleListener;
import com.kzd.sdk.core.KZD_SdkManager;
import com.kzd.sdk.entity.KZD_SdkPayParams;
import com.kzd.sdk.entity.KZD_SdkRoleParams;

import java.text.SimpleDateFormat;
import java.util.Locale;

public class MainJavaActivity extends AppCompatActivity {

    private String mSmallId = null;

    private String getTime() {
        return new SimpleDateFormat(("yyyy-MM-dd HH:mm:ss"), Locale.CHINA).format(System.currentTimeMillis());
    }

    private EditText etTestLoginCallbackInfo, etTestPayCallbackInfo, etTestRoleCallbackInfo;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        KZD_SdkManager.onCreate(this);

        etTestLoginCallbackInfo = findViewById(R.id.etTestLoginCallbackInfo);
        etTestPayCallbackInfo = findViewById(R.id.etTestPayCallbackInfo);
        etTestRoleCallbackInfo = findViewById(R.id.etTestRoleCallbackInfo);

        //设置回调监听器(集体设置)
        KZD_SdkManager.setListener(new KZD_OnLoginListener() {//登录监听
            @Override
            public void onLoginSuccess(String uid, String sessionId) {
                //登录成功 uid用户小号id，sessionId后台服务器交互
                mSmallId = uid;
                String hint = etTestLoginCallbackInfo.getText() + "登录成功:\t"
                        + getTime() + "\nuid=" + uid + "\nsessionId=" + sessionId + "\n\n";
                etTestLoginCallbackInfo.setText(hint);
            }

            @Override
            public void onLoginSwitch(int type) {
                //登录切换 type=1大号切换，type=2小号切换
                String hint = etTestLoginCallbackInfo.getText() + "账号切换:\t"
                        + getTime() + "\ntype=" + type;
                if (type == 1) {
                    hint = hint + "，大号切换\n\n";
                } else if (type == 2) {
                    hint = hint + "，小号切换\n\n";
                }
                etTestLoginCallbackInfo.setText(hint);
            }

            @Override
            public void onLoginError(String error) {
                //登录异常 error异常消息
                String hint = etTestLoginCallbackInfo.getText() + "登录异常:\t"
                        + getTime() + "\nerror=" + error + "\n\n";
                etTestLoginCallbackInfo.setText(hint);
            }
        }, new KZD_OnPayListener() {//支付监听
            @Override
            public void onPayingSuccess(String text) {
                //支付成功 text消息
                String hint = etTestPayCallbackInfo.getText() + "支付成功:\t"
                        + getTime() + "\ntext=" + text + "\n\n";
                etTestPayCallbackInfo.setText(hint);
            }

            @Override
            public void onPayingError(String error) {
                //支付异常 error异常消息
                String hint = etTestPayCallbackInfo.getText() + "支付异常:\t"
                        + getTime() + "\nerror=" + error + "\n\n";
                etTestPayCallbackInfo.setText(hint);
            }
        }, new KZD_OnRoleListener() {//角色上传监听
            @Override
            public void onRolesSuccess(String text) {
                //上传成功 text消息
                String hint = etTestRoleCallbackInfo.getText() + "角色上传成功:\t"
                        + getTime() + "\ntext=" + text + "\n\n";
                etTestRoleCallbackInfo.setText(hint);
            }

            @Override
            public void onRolesError(String error) {
                //上传异常 error异常消息
                String hint = etTestRoleCallbackInfo.getText() + "角色上传异常:\t"
                        + getTime() + "\nerror=" + error + "\n\n";
                etTestRoleCallbackInfo.setText(hint);
            }
        });

        //设置回调监听器(单独设置)
//        KZD_SdkManager.setLoginListener(new KZD_OnLoginListener() {//登录监听
//            @Override
//            public void onLoginSuccess(String uid, String sessionId) {
//                //登录成功 uid用户小号id，sessionId后台服务器交互
//            }
//
//            @Override
//            public void onLoginSwitch(int type) {
//                //登录切换 type=1大号切换，type=2小号切换
//            }
//
//            @Override
//            public void onLoginError(String error) {
//                //登录异常 error异常消息
//            }
//        });
//
//        KZD_SdkManager.setPayListener(new KZD_OnPayListener() {//支付监听
//            @Override
//            public void onPayingSuccess(String text) {
//                //支付成功 text消息
//            }
//
//            @Override
//            public void onPayingError(String error) {
//                //支付异常 error异常消息
//            }
//        });
//
//        KZD_SdkManager.setRoleListener(new KZD_OnRoleListener() {//角色上传监听
//            @Override
//            public void onRolesSuccess(String text) {
//                //上传成功 text消息
//            }
//
//            @Override
//            public void onRolesError(String error) {
//                //上传异常 error异常消息
//            }
//        });

        //登录
        findViewById(R.id.btnTestLogin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!KZD_SdkManager.isNotLogin()) {
                    Toast.makeText(MainJavaActivity.this, "已登录", Toast.LENGTH_SHORT).show();
                    return;
                }
                //如已设置登录监听则调用如下
                KZD_SdkManager.login(MainJavaActivity.this);
                //如未设置登录监听则调用如下
//                KZD_SdkManager.login(mActivity, new KZD_OnLoginListener() {
//                    @Override
//                    public void onLoginSuccess(String uid, String sessionId) {
//                        //登录成功 uid用户小号id，sessionId后台服务器交互
//                    }
//
//                    @Override
//                    public void onLoginSwitch(int type) {
//                        //登录切换 type=1大号切换，type=2小号切换
//                    }
//
//                    @Override
//                    public void onLoginError(String error) {
//                        //登录异常 error异常消息
//                    }
//                });
            }
        });

        //支付
        findViewById(R.id.btnTestPay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //支付参数信息
                KZD_SdkPayParams payParams = new KZD_SdkPayParams();
                payParams.setSmallId(mSmallId);//登录成功后返回的uid(小号id)
                payParams.setCpOrderId("0");//游戏订单,没有直接可以写0
                payParams.setRoleId("1");//角色id
                payParams.setServerId("1");//区服id
                payParams.setMoney("98");//支付金额
                payParams.setExtInfo("");//拓展参数

                String hint = etTestPayCallbackInfo.getText() + "支付参数信息:\t"
                        + getTime() + "\n" + payParams.toString() + "\n\n";
                etTestPayCallbackInfo.setText(hint);

                //已设置支付监听则调用
                KZD_SdkManager.paying(MainJavaActivity.this, payParams);
                //未设置支付监听则调用
//                KZD_SdkManager.paying(mActivity, payParams, new KZD_OnPayListener() {
//                    @Override
//                    public void onPayingSuccess(String text) {
//                        //支付成功 text消息
//                    }
//
//                    @Override
//                    public void onPayingError(String error) {
//                        //支付异常 error异常消息
//                    }
//                });
            }
        });

        //角色上传
        findViewById(R.id.btnTestRole).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //角色上传参数信息
                KZD_SdkRoleParams roleParams = new KZD_SdkRoleParams();
                roleParams.setSmallId(mSmallId);//登录成功后返回的uid(小号id)
                roleParams.setRoleName("测试角色名1");//角色名称
                roleParams.setRoleId("1");//角色id
                roleParams.setRoleLevel("1");//角色等级
                roleParams.setGameName("测试游戏名");//游戏名称
                roleParams.setServerId("1");//区服id
                roleParams.setServerName("测试1区");//区服名称

                String hint = etTestRoleCallbackInfo.getText() + "角色上传参数信息:\t"
                        + getTime() + "\n" + roleParams.toString() + "\n\n";
                etTestRoleCallbackInfo.setText(hint);
                //已设置角色上传监听则调用
                KZD_SdkManager.roles(MainJavaActivity.this, roleParams);
                //未设置角色上传监听则调用
//                KZD_SdkManager.roles(mActivity, roleParams, new KZD_OnRoleListener() {//角色上传监听
//                    @Override
//                    public void onRolesSuccess(String text) {
//                        //上传成功 text消息
//                    }
//
//                    @Override
//                    public void onRolesError(String error) {
//                        //上传异常 error异常消息
//                    }
//                });
            }
        });

        //退出登录
        findViewById(R.id.btnTestExit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KZD_SdkManager.exit(MainJavaActivity.this);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        KZD_SdkManager.onActivityResult(this, requestCode, resultCode, data);
        //KZD_SdkManager.openFloatView(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        KZD_SdkManager.onRestart(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        KZD_SdkManager.onStart(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        KZD_SdkManager.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        KZD_SdkManager.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        KZD_SdkManager.onStop(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        KZD_SdkManager.onDestroy(this);
//        KZD_SdkManager.closeFloatView(this);
    }
}
